#include "head_2.h"

int head_insert(linknode *H, sendEnv value);
int frist_insert(linknode *H, linknode *E, sendEnv value);
linknode * list_create();
void * pthread_transfer (void * sockfd);

int main(int argc, char *argv[]) {


	int listenfd;
	struct sockaddr_in myaddr;
	pthread_t pthread_client_request;
	pthread_t pthread_refresh;
	pthread_t pthread_sqlite;
	pthread_t id_transfer;
	pthread_t pthread_analysis;
	pthread_t pthread_uart_cmd;
	pthread_t pthread_sms;
	pthread_t pthread_led;
	pthread_t pthread_camera;
	pthread_t phtread_buzzer;
	linknode *H = NULL;
	linknode *E = NULL;
	void *retval;
	
	init(&envbuff);
	
	E = H = list_create();

	if (argc < 3) {
		printf("Usage : %s <ip> <port>\n", argv[0]);
		exit(-1);
	}

	listenfd = socket(PF_INET, SOCK_STREAM, 0);
	if(listenfd < 0) {
		perror("fail to socket");
		exit(-1);
	}
	
	bzero(&myaddr, sizeof(myaddr));
	myaddr.sin_family = PF_INET;
	myaddr.sin_port = htons(atoi(argv[2]));
	myaddr.sin_addr.s_addr = inet_addr(argv[1]);
	if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0) {
		perror("fail to bind");
		exit(-1);
	}

	if (listen(listenfd, 5) < 0) {
		perror("fail to listen");
		exit(-1);
	}
	
	
	if((sockfd = accept(listenfd, NULL, NULL)) < 0){
		perror("fail to accept");
			exit(-1);
	}
		
	pthread_t id_transfer;
		
	if(pthread_create(&id_transfer, NULL, pthread_transfer,(void *)sockfd) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
		
	
	
}


void init(struct prodcons *b) {		//初始化一切

	/*互斥锁*/
	pthread_mutex_init(&b->mutex_slinklist, NULL);
	pthread_mutex_init(&b->mutex_sqlite, NULL);
	pthread_mutex_init(&b->mutex_analysis, NULL);
	pthread_mutex_init(&b->mutex_uart_cmd, NULL);
	pthread_mutex_init(&b->mutex_client_request, NULL);
	pthread_mutex_init(&b->mutex_infrared, NULL);
	pthread_mutex_init(&b->mutex_buzzer, NULL);
	pthread_mutex_init(&b->mutex_led, NULL);
	pthread_mutex_init(&b->mutex_camera, NULL);
	pthread_mutex_init(&b->mutex_sms, NULL);
	pthread_mutex_init(&b->mutex_refresh, NULL);
	pthread_mutex_init(&b->mutex_refresh_updata, NULL);
	pthread_mutex_init(&b->mutex_global, NULL);
	pthread_mutex_init(&b->mutex_linklist, NULL);
	
	/*条件变量*/
	pthread_cond_init(&b->cond_sqlite, NULL);
	pthread_cond_init(&b->cond_analysis, NULL);
	pthread_cond_init(&b->cond_uart_cmd, NULL);
	pthread_cond_init(&b->cond_client_request, NULL);
	pthread_cond_init(&b->cond_infrared, NULL);
	pthread_cond_init(&b->cond_buzzer, NULL);
	pthread_cond_init(&b->cond_led, NULL);
	pthread_cond_init(&b->cond_camera, NULL);
	pthread_cond_init(&b->cond_sms, NULL);
	pthread_cond_init(&b->cond_refresh, NULL);
}

void * pthread_transfer (void * sockfd) {

	sendEnv info;
	int i = 0;
	
	while(1) {
	
		recv(sockfd, &info, sizeof(sendEnv), 0);
		if (i == 0) {
			frist_insert(H, E, &info);
			i++;
		} else {
			head_insert(H, &info);
		}
	}

}

linknode * list_create() {

	linknode *H = NULL;

	if((H = (linknode *)malloc(sizeof(linknode))) == NULL)
	{
		printf("fail to malloc\n");
		return NULL;
	}

	H->next = NULL;
	H->pro = NULL;
	return H;
}

int frist_insert(linknode *H, linknode *E, sendEnv value) {

	E->env = value;
	E->next = H->next;
	E->pro = H;
	H->next = E;

	return 0;
}

int head_insert(linknode *H, sendEnv value) {
	
	linknode * p = NULL;
	
	if((p = (linknode *)malloc(sizeof(linknode))) == NULL) {
		printf("fail to malloc\n");
		
		return -1;
	}

	H->next->pro = p;
	p->env = value;
	p->next = H->next;
	p->pro = H;
	H->next = p;

	return 0;
}
















